package com.bgs.pojo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bgs.tool.AliOss;
import com.bgs.tool.R;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class QrCode {

    public static MultipartFile idtest(String ss) throws IOException {
        String AppKey = "a0b9b5bd4d1ca5dff25285f1c2f25bbc";
        String Text= ss;
        String url = "http://apis.juhe.cn/qrcode/api?key=" + AppKey + "&text="+Text;
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        //调用工具类 返回API返回的Json
        String str = client(url, HttpMethod.GET, params);
        //使用的是alibaba的JSON工具类
        JSONObject object = JSON.parseObject(str);
        String base64 = object.getJSONObject("result").get("base64_image").toString();
        //路径自己修改，这里图方便直接写死了
        BaseTest.GenerateImage(base64, "D:\\a.jpg");
        File pdfFile = new File("D:\\a.jpg");
        FileInputStream fileInputStream = new FileInputStream(pdfFile);
        MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
                ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        return multipartFile;
    }

    public static String client(String url, HttpMethod method, MultiValueMap<String, String> parms) {
        RestTemplate template = new RestTemplate();
        ResponseEntity<String> response = template.getForEntity(url, String.class);
        return response.getBody();
    }
}
